package com.jyw.business.comp.service.impl;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Year;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.jyw.business.comp.domain.dto.HolidaySetDto;
import com.jyw.common.utils.DateUtils;
import org.springframework.stereotype.Service;
import com.jyw.business.comp.mapper.CompScheduleHolidayMapper;
import com.jyw.business.comp.domain.CompScheduleHoliday;
import com.jyw.business.comp.service.ICompScheduleHolidayService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 综合-日历配置Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-25
 */
@Service
public class CompScheduleHolidayServiceImpl extends ServiceImpl<CompScheduleHolidayMapper,CompScheduleHoliday> implements ICompScheduleHolidayService
{
    @Resource
    private CompScheduleHolidayMapper compScheduleHolidayMapper;

    /**
     * 查询综合-日历配置
     * 
     * @param id 综合-日历配置主键
     * @return 综合-日历配置
     */
    @Override
    public CompScheduleHoliday selectCompScheduleHolidayById(Long id)
    {
        return compScheduleHolidayMapper.selectCompScheduleHolidayById(id);
    }

    /**
     * 查询综合-日历配置列表
     * 
     * @param compScheduleHoliday 综合-日历配置
     * @return 综合-日历配置
     */
    @Override
    public List<CompScheduleHoliday> selectCompScheduleHolidayList(CompScheduleHoliday compScheduleHoliday)
    {
        return compScheduleHolidayMapper.selectCompScheduleHolidayList(compScheduleHoliday);
    }

    /**
     * 新增综合-日历配置
     * 
     * @param compScheduleHoliday 综合-日历配置
     * @return 结果
     */
    @Override
    public int insertCompScheduleHoliday(CompScheduleHoliday compScheduleHoliday)
    {
        return compScheduleHolidayMapper.insertCompScheduleHoliday(compScheduleHoliday);
    }

    /**
     * 修改综合-日历配置
     * 
     * @param compScheduleHoliday 综合-日历配置
     * @return 结果
     */
    @Override
    public int updateCompScheduleHoliday(CompScheduleHoliday compScheduleHoliday)
    {
        return compScheduleHolidayMapper.updateCompScheduleHoliday(compScheduleHoliday);
    }

    /**
     * 批量删除综合-日历配置
     * 
     * @param ids 需要删除的综合-日历配置主键
     * @return 结果
     */
    @Override
    public int deleteCompScheduleHolidayByIds(Long[] ids)
    {
        return compScheduleHolidayMapper.deleteCompScheduleHolidayByIds(ids);
    }

    /**
     * 删除综合-日历配置信息
     * 
     * @param id 综合-日历配置主键
     * @return 结果
     */
    @Override
    public int deleteCompScheduleHolidayById(Long id)
    {
        return compScheduleHolidayMapper.deleteCompScheduleHolidayById(id);
    }

    /**
     * 查询该年度是否生成了日历,如果没有生成先初始化
     * @param yearStr
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<CompScheduleHoliday> yearList(String yearStr) {
        List<CompScheduleHoliday> list = queryHolidayByYear(yearStr);
        //已经存在那么直接返回
        if(list.size()>0){
            return list;
        }
        //不存在初始化今年的数据
        Year year = Year.parse(yearStr);
        //获取年份的第一天和最后一天
        LocalDate start = year.atDay(1);
        LocalDate end = year.atDay(year.length());
        //声明周末
        List<DayOfWeek> weekend = Arrays.asList(DayOfWeek.SATURDAY, DayOfWeek.SUNDAY);
        //遍历每一天
        for (LocalDate date = start; !date.isAfter(end); date = date.plusDays(1)) {
            int holidayType = 0;
            //判断是否是周末
            if(weekend.contains(date.getDayOfWeek())){
                holidayType = 1;
            }
            Date holidayTime = DateUtils.toDate(date);
            CompScheduleHoliday holiday = new CompScheduleHoliday();
            holiday.setHolidayTime(holidayTime);
            holiday.setHolidayType(holidayType);
            save(holiday);
        }
        //重新查询
        list = queryHolidayByYear(yearStr);
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setHoliday(HolidaySetDto dto) {
        //查询所有日期
        List<CompScheduleHoliday> list = lambdaQuery()
                .in(CompScheduleHoliday::getHolidayTime, dto.getHolidayTimeList())
                .list();
        list = list.stream().map(data->{
            //设置日期类型
            if(dto.getSet().equals(0)){
                //取消节假日
                if(data.getHolidayType().equals(3)){
                    //节假日+周末设置为周末
                    data.setHolidayType(1);
                } else if (data.getHolidayType().equals(2)) {
                    //节假日 设置为工作日
                    data.setHolidayType(0);
                }
            }else {
                //设置为节假日
                if(data.getHolidayType().equals(1)){
                    //周末设置为节假日+周末
                    data.setHolidayType(3);
                } else if (data.getHolidayType().equals(0)) {
                    //工作日设置为节假日
                    data.setHolidayType(2);
                }
            }
            return data;
        }).collect(Collectors.toList());
        //批量修改
        updateBatchById(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void clean(String year) {
        List<CompScheduleHoliday> compScheduleHolidays = queryHolidayByYear(year);
        compScheduleHolidays = compScheduleHolidays.stream()
                //查找设为节假日的数据
                .filter(data->data.getHolidayType().equals(2)||data.getHolidayType().equals(3))
                //取消节假日
                .map(data->{
                    if(data.getHolidayType().equals(2)){
                        data.setHolidayType(0);
                    } else if (data.getHolidayType().equals(3)) {
                        data.setHolidayType(1);
                    }
                    return data;
                }).collect(Collectors.toList());
        //修改日历
        updateBatchById(compScheduleHolidays);
    }

    private List<CompScheduleHoliday> queryHolidayByYear(String yearStr){
        return lambdaQuery()
                .apply("year(holiday_time) = {0}", yearStr)
                .orderByAsc(CompScheduleHoliday::getHolidayTime)
                .list();
    }
}
